package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController{
    @Autowired
    private CategoryService categoryService;
    /**
     * 展示每页分类数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> categoryPage(Integer page,Integer pageSize) {
        Page<Category> categoryPage = categoryService.page(new Page<>(page, pageSize));
        return R.success(categoryPage);
    }

    /**
     * 新增分类数据
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("新增成功");
    }

    /**
     * 修改分类数据
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 删除分类数据
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        //根据id删除分类
        categoryService.remove(id);
        return R.success("删除成功");
    }

    /**
     *展示分类类别
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        //菜品分类条件
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<Category>().eq(category.getType()!=null,Category::getType, category.getType());
        //添加排序条件
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }

}
